package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.view.EmbeddedGridView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.rocksdata.mc.entity.NewsTagInfo;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/8.
 */
@ContentView(R.layout.activity_news_tag_settings)
public class NewsTagSettingsActivity extends TitleBarActivity {
    @ViewInject(R.id.gv_news_tag_selected)
    private EmbeddedGridView gvSelected;
    @ViewInject(R.id.gv_news_tag_normal)
    private EmbeddedGridView gvNormal;

    private List<NewsTagInfo> newsTagList;
    private List<NewsTagInfo> selectedList = new ArrayList<NewsTagInfo>();
    private List<NewsTagInfo> normalList = new ArrayList<NewsTagInfo>();
    private NewsTagGridAdapter selectedAdapter, normalAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("设置");
        setTitleRightText("完成");

        newsTagList = (List<NewsTagInfo>) getIntent().getSerializableExtra(Constant.EXTRA_KEY_NEWS_TAG);
        for(NewsTagInfo bean: newsTagList){
            if(bean.isSelected()){
                selectedList.add(bean);
            }else{
                normalList.add(bean);
            }
        }

        selectedAdapter = new NewsTagGridAdapter(this, selectedList);
        gvSelected.setAdapter(selectedAdapter);

        normalAdapter = new NewsTagGridAdapter(this, normalList);
        gvNormal.setAdapter(normalAdapter);

        setTitleRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<NewsTagInfo> list = new ArrayList<NewsTagInfo>();
                list.addAll(selectedList);
                list.addAll(normalList);
                Intent intent = new Intent();
                intent.putExtra(Constant.EXTRA_KEY_NEWS_TAG, (Serializable) list);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

    @Event(value = {R.id.gv_news_tag_selected, R.id.gv_news_tag_normal}, type = AdapterView.OnItemClickListener.class)
    private void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.gv_news_tag_selected:
                NewsTagInfo bean = selectedAdapter.getItem(position);
                bean.setSelected(false);
                selectedList.remove(bean);
                normalList.add(bean);
                break;
            case R.id.gv_news_tag_normal:
                NewsTagInfo bean1 = normalAdapter.getItem(position);
                bean1.setSelected(true);
                normalList.remove(bean1);
                selectedList.add(bean1);
                break;
        }
        selectedAdapter.notifyDataSetChanged();
        normalAdapter.notifyDataSetChanged();
    }

    class NewsTagGridAdapter extends AbsAdapter<NewsTagInfo> {

        public NewsTagGridAdapter(Context context, List<NewsTagInfo> datas) {
            super(context, datas, R.layout.item_news_tag_grid);
        }

        @Override
        public void convert(AdapterViewHolder holder, NewsTagInfo data) {
            TextView tvTag = holder.getView(R.id.tv_news_tag);
            tvTag.setText(data.getProgramName());
        }
    }
}
